#-*-coding=utf-8-*-
from google.appengine.api import users
from django.http import HttpResponseRedirect
LOGIN_OR_CREATE = "/login_or_reg"
NOT_ADMIN_USER = "/"

def login_required(func=None):
    def _login_required(view_func):
        def new_view_func(request, *args, **argkw):
            user = users.get_current_user()
            if user:
                return view_func(request, *args, **argkw)
            else:
                return HttpResponseRedirect(users.create_login_url(LOGIN_OR_CREATE+"?next="+request.path+"?"+request.META["QUERY_STRING"]))
        return new_view_func
    if func:
        return _login_required(func)
    return _login_required

#未完成测试
def admin_required(func=None):
    def _admin_required(view_func):
        def new_view_func(request, *args, **argkw):
            user = users.get_current_user()
            if user and users.is_current_user_admin():
                return view_func(request, *args, **argkw)
            else:
                return HttpResponseRedirect(NOT_ADMIN_USER)
        return new_view_func
    if func:
        return _admin_required(func)
    return _admin_required